package com.kingdee.pda.ui.fast_allot;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.SpeakUtils;
import com.example.basecommon.view.swipe_refresh.SwipeRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.adapter.FastAllotOrderListAdapter;
import com.kingdee.pda.entity.FastAllotResponseBean;
import com.kingdee.pda.entity.FastAllotTaskResquestBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.priority.PriorityUtils;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.CommonUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * 快速调拨单列表
 * Created by dlj
 * DATE 2021/9/23
 * TIME 17:20
 */
public class FastAllotOrderListActivity extends BaseTitleActivity implements View.OnKeyListener {

    private SwipeRecyclerView recyclerView;
    private FastAllotOrderListAdapter mAdapter;
    private EditText et_orderNo;
    private List<FastAllotResponseBean> data = new ArrayList<>();
    Button btn_creat;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, FastAllotOrderListActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_query_fastallot_order);
    }

    @Override
    protected void onResume() {
        super.onResume();

        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("快速调拨");
        et_orderNo = findViewById(R.id.et_orderNo);

        recyclerView = findViewById(R.id.list_view);
        recyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setRefreshEnable(false);
        recyclerView.setLoadMoreEnable(false);

        setEditTextScannerEvents(et_orderNo, this);
        findViewById(R.id.btn_query).setOnClickListener(this);
        btn_creat = findViewById(R.id.btn_creat);
        btn_creat.setOnClickListener(this);

        if (PriorityUtils.INSTANCE.getCreatetransferdoc()){
            btn_creat.setVisibility(View.VISIBLE);
        }
        mAdapter = new FastAllotOrderListAdapter(this, data);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                FastAllotResponseBean fastAllotResponseBean = data.get(position);
                FastAllotOperateTaskActivity.actionStart(FastAllotOrderListActivity.this, fastAllotResponseBean);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        CommonUtil.requestFocusAndNoClearText(et_orderNo);
        getOrderList("");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_query:
                String queryText = et_orderNo.getText().toString().trim();
                if (!TextUtils.isEmpty(queryText)) {
                    getOrderList(queryText);
                }
                break;
            case R.id.btn_creat:
                FastAllotSelectWarehouseActivity.actionStart(this);
                break;
        }
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                getOrderList(input);
                return true;
            }
        }
        return false;
    }

    private void getOrderList(String queryText) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("billnoAndItemskucode", queryText);
        MyObserver myObserver = new MyObserver(this) {
            @Override
            public void onSuccess(BaseResponse result) {
                //后台返回格式有问题，我们手动处理一下
                if (result.data instanceof List) {
                    Type type = new TypeToken<List<FastAllotResponseBean>>() {
                    }.getType();
                    Gson gson = new Gson();
                    List<FastAllotResponseBean> resultList = gson.fromJson(gson.toJson(result.data), type);
                    List<FastAllotResponseBean> list = new ArrayList<>();
                    for (FastAllotResponseBean bean :resultList) {
                        if (getCurrentWarehouse().getId() == bean.getWarehouseout()) {
                            list.add(bean);
                        }
                    }
                    //按时间进行排序
                    Collections.sort(list, new Comparator<FastAllotResponseBean>() {
                        public int compare(FastAllotResponseBean e1, FastAllotResponseBean e2) {
                            int a = e2.getCreatetime().compareTo(e1.getCreatetime());
                            return a > 0 ? 1 : -1;
                        }
                    });
                    FastAllotOrderListActivity.this.data.clear();
                    FastAllotOrderListActivity.this.data.addAll(list);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                SpeakUtils.speakWithToast(FastAllotOrderListActivity.this, msg, false, false);
            }
        };
        HttpUtils.request(Api.class)
                .getFastAllotOrderList(ConstantUtil.GET_FASTALLOT_ORDERLIST, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);
    }
}
